<?php
//自动生成,请根据需要修改
namespace app\good\controller;
use app\common\controller\Base;
/**
* @title 商品信息页面（模块04-01）
* @description 商品信息说明
* @group 商品（04）
* @header name:model_id require:0 default:04-01 desc:模块
* @groupremark 已使用错误码：1-04-01-0001 校验失败 1-04-01-0002 添加失败 1-04-01-0003 编辑校验失败 1-04-01-0004 编辑失败 1-04-01-0005 获取详情失败  1-04-01-0006 获取列表失败 1-04-01-0007获取单个详情失败 1-04-01-0008 删除失败
*/
class Good extends Base{
		//测试
	public function test(){
		return 'test 111';
	}
	
	/**
	* @title 添加商品信息
	* @description 添加商品信息
	* @author 李锐志
	* @url /good/good/add
	* @method *
	*
	* @header name:token require:1 default: desc:token
	*
	* @param name:name type:varchar require:0 other: desc:产品名称
	* @param name:title type:varchar require:0 other: desc:产品标题
	* @param name:brand type:varchar require:0 other: desc:品牌
	* @param name:specification type:varchar require:0 other: desc:规格
	* @param name:origin type:varchar require:0 other: desc:产地
	* @param name:discount type:float require:0 other: desc:折扣
	* @param name:original_price type:decimal require:0 other: desc:原价
	* @param name:total_sales_price type:decimal require:0 other: desc:售价
	* @param name:main_img_id type:int require:0 other: desc:主图
	* @param name:append_img_ids type:varchar require:0 other: desc:附图
	* @param name:details type:text require:0 other: desc:商品详情
	* @param name:sys_sign type:varchar require:0 other: desc:系统
	* @return data:商品信息@!
	* @data 	id:id	name:产品名称	title:产品标题	brand:品牌	specification:规格	origin:产地	discount:折扣	original_price:原价	total_sales_price:售价	main_img_id:主图	append_img_ids:附图	details:商品详情	sys_sign:系统
	* @return error_id:1-04-01-0001 校验失败  1-04-01-0002 添加失败
	*/
	public function add(){
		$data=input();
		/**
		*$validate = new \app\good\validate\Good();
		*$validate->scene('add');
		*$result = $validate->batch(true)->check($data);
		*if ($result !== true) {
		*$msg['msg']='校验失败';
		*$msg['validate']=$validate->getError();
		*$msg['error_id']='1-04-01-0001';
		*return $this->errorJson($msg);
		*}
		**/
		$model=new \app\good\model\Good();
		$flag = $model->save($data);
		if($flag){
			$msg['msg']='添加成功';
			$msg['data']=$model->get($model['id']);
			return $this->successJson($msg);
		}else{
			$msg['error_id']='1-04-01-0002';
			$msg['msg']='添加失败';
			return $this->errorJson($msg);
		}
	}
		/**
	* @title 编辑商品信息
	* @description 编辑商品信息
	* @author 李锐志
	* @url /good/good/edit
	* @method *
	*
	* @header name:token require:1 default: desc:token
	*
	* @param name:id type:int require:0 other: desc:id
	* @param name:name type:varchar require:0 other: desc:产品名称
	* @param name:title type:varchar require:0 other: desc:产品标题
	* @param name:brand type:varchar require:0 other: desc:品牌
	* @param name:specification type:varchar require:0 other: desc:规格
	* @param name:origin type:varchar require:0 other: desc:产地
	* @param name:discount type:float require:0 other: desc:折扣
	* @param name:original_price type:decimal require:0 other: desc:原价
	* @param name:total_sales_price type:decimal require:0 other: desc:售价
	* @param name:main_img_id type:int require:0 other: desc:主图
	* @param name:append_img_ids type:varchar require:0 other: desc:附图
	* @param name:details type:text require:0 other: desc:商品详情
	* @param name:sys_sign type:varchar require:0 other: desc:系统
	* @return data:商品信息@!
	* @data 	id:id	name:产品名称	title:产品标题	brand:品牌	specification:规格	origin:产地	discount:折扣	original_price:原价	total_sales_price:售价	main_img_id:主图	append_img_ids:附图	details:商品详情	sys_sign:系统
	* @return error_id:1-04-01-0003 校验失败 1-04-01-0004 修改失败
	*/
	public function edit(){
		$data=input();
		$validate = new \app\good\validate\Good();
		$validate->scene('edit');
		$result = $validate->batch(true)->check($data);
		if ($result !== true) {
			$msg['msg']='校验失败';
			$msg['validate']=$validate->getError();
			$msg['error_id']='1-04-01-0003';
			return $this->errorJson($msg);
		}
		$model=new \app\good\model\Good();
		$flag = $model->edit($data);
		if($flag!==false){
			$msg['msg']='修改成功';
			$msg['data']=$model->get($data['id']);
			return $this->successJson($msg);
		}else{
			$msg['error_id']='1-04-01-0004';
			$msg['msg']='修改失败';
			return $this->errorJson($msg);
		}
	}
		
	/**
	* @title 删除商品信息
	* @description 删除商品信息
	* @author 李锐志
	* @url /good/good/delete
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:int|array require:1 other: desc:要删除的id
	* @return error_id:1-04-01-0008 删除失败
	*/
	public function delete(){
		$id=input('id');
		$model=new \app\good\model\Good();
		$flag = $model->destroy($id);
		if($flag){
			$msg['msg']='删除成功';
			return $this->successJson($msg);
		}else{
			$msg['msg']='删除失败';
			$msg['error_id']='1-04-01-0008';
			return $this->errorJson($msg);
		}
	}
	    
    /**
    * @title 单个查询商品信息
    * @description 单个查询商品信息
    * @author 李锐志
    * @url /good/good/info
    * @method *
    *
    * @param name:token require:1 default: desc:token
    *
    * @param name:id type:int require:1 other: desc:要查询的id
    * @return data:商品信息@!
    * @data 	id:id	name:产品名称	title:产品标题	brand:品牌	specification:规格	origin:产地	discount:折扣	original_price:原价	total_sales_price:售价	main_img_id:主图	append_img_ids:附图	details:商品详情	sys_sign:系统
    * @return error_id:1-04-01-0005 校验失败 1-04-01-0007 获取失败
    */
    public function info(){
        $id=input('id');
        $data['id']=$id;
        $validate = new \app\good\validate\Good();
        $validate->scene('info');
        $result = $validate->batch(true)->check($data);
        if ($result !== true) {
        $msg['msg']='校验失败';
        $msg['validate']=$validate->getError();
        $msg['error_id']='1-04-01-0005';
        return $this->errorJson($msg);
        }
        $model=new \app\good\model\Good();
        $info = $model->find($id);
        if($info){
            $msg['msg']='获取成功';
            $msg['data']=$info;
            return $this->successJson($msg);
        }else{

            $msg['msg']='获取失败';
            $msg['error_id']='1-04-01-0007';
            return $this->successJson($msg);
        }
    }
		
	/**
	* @title 查询列表商品信息
	* @description 查询列表商品信息
	* @author 李锐志
	* @url /good/good/get_list
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:int require:1 other: desc:要查询的id
	* @return list:商品信息@
	* @list 	id:id	name:产品名称	title:产品标题	brand:品牌	specification:规格	origin:产地	discount:折扣	original_price:原价	total_sales_price:售价	main_img_id:主图	append_img_ids:附图	details:商品详情	sys_sign:系统
	* @return error_id:1-04-01-0006 校验失败
	*/
	public function get_list(){
		$model=new \app\good\model\Good();
		$Fields=$model->getTableFields();
		$data=\think\facade\Request::only($Fields);
		/**
		*$validate = new \app\good\validate\Good();
		*$validate->scene('lists');
		*$result = $validate->batch(true)->check($data);
		*if ($result !== true) {
		*$msg['msg']='校验失败';
		*$msg['validate']=$validate->getError();
		*$msg['error_id']='1-04-01-0006';
		*return $this->errorJson($msg);
		*}
		**/
		//每页显示10条数据
		$list = $model->where($data)->paginate();
		$msg['list']=$list;
		return $this->successJson($msg);
	}
}
